var filename="";

window.zipGenerate = function(successZipping, params) {
	if (params != null)
		cordova.exec(successZipping, failZipping, "ZipGenerate", null, params);
	else
		console.log("Error: params cannot be null");
};

function doGenerateZip() {
    filename = device.uuid +"-"+localStorage.getItem("fileTime")+".zip";
	var params = new Array();
	var zipFile = filename;// absolute path needs to be
												// specified
	params[0] = zipFile;

	window.zipGenerate(successZipping, params);
}

function failZipping(err) {
	alert("No data found!");
	Popup.hide('modal');
	//processDeleteFile();
}

function successZipping(result) {
	//
	var url2="https://intensecontrol.com/pensonic/main/tablet_upload_act.jsp";
	var filePath=localStorage.getItem("rootDir")+"/iControl_upload/"+device.uuid+"-"+localStorage.getItem("fileTime")+".zip";
    var ft = new FileTransfer();
	var options = new FileUploadOptions();
    options.fileKey="file";
    options.fileName=filePath.substr(filePath.lastIndexOf('/')+1);//hard coded for now
    options.mimeType="multipart/x-zip";
	
	var params = {};
    params.value1 = "test";
    params.value2 = "param";

    options.params = params;

    ft.upload(filePath, url2, uploadSuccess, uploadFailed, options,true);
}

function processDeleteFile() {
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotDeleteFS, fail);
}

function processDeleteFile2() {
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotDeleteFS2, fail);
}

function gotDeleteFS(fileSystem) {
	fileSystem.root.getDirectory("iControl_upload", {
		create : false,
		exclusive : false
	}, getDeleteDirSuccess, fail);
}

function gotDeleteFS2(fileSystem) {
	fileSystem.root.getDirectory("iControl", {
		create : false,
		exclusive : false
	}, getDeleteDirSuccess2, fail);
}


function getDeleteDirSuccess(dirEntry) {
	dirEntry.getFile(filename, null, gotDeleteFileEntry, fail);
	
}

function getDeleteDirSuccess2(dirEntry) {
	dirEntry.removeRecursively();
	createDir("iControl");
}

function gotDeleteFileEntry(fileEntry) {
	fileEntry.remove(successHandlerI, fail);
}

function successHandlerI() {
	Popup.hide('modal');
}

function fail(error) {
	console.log(error.code);
	Popup.hide('modal');
}

function uploadSuccess(r)
{
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
    
    updateSyncSuccess();
}

function uploadFailed(error)
{
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
	Popup.hide('modal');
}

function createDir(dirname) {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
        fileSys.root.getDirectory(dirname, {
            create : true,
            exclusive : false
        }, function(directory) {
            console.log("Directory has been created");
        }, createError);
    }, createError);
}

function createError(error) {
    alert(error.code);
}
